<?php

/*	Vulpes-backend - Web-based managegent for wireless internet providers.
	Copyright (C) 2007,2008 LinFox Serviços em Informática LTDA.

	This file is part of Vulpes-backend.

	Vulpes-backend is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes-backend is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 	Please note that any front-end (client application) that is distributed
	along with Vulpes-backend, such as the Vulpes frontend by LinFox,
	is subject to it's correspending licene. */

require_once 'controller.php';
require_once 'contract_dao.php';
require_once 'client_dao.php';

class contract_ctl extends controller
{
	protected $mode = 'contracts';
	protected $model = 'contract';
	protected $dao = 'contract_dao';
	
	function __construct()
	{
		parent::__construct();
	}

	function show_all()
	{
		$contract_dao = new contract_dao($this->db);
		if (get('client_id')) {
			$contracts = $contract_dao->fetch_client(get('client_id'));
		} else {
			$contracts = $contract_dao->fetch_all(get('search'));
		}
		$result = '<contracts>';
		foreach ($contracts as $contract) {
			$result .= $contract->xml_iterate('contract');
		}
		$result .= '</contracts>';
		return $result;
	}
	
	function insert()
	{
		$client_dao = new client_dao();
		$client = $client_dao->fetch(get($client_dao->key));
		$contract_dao = new contract_dao();
		$contract = $contract_dao->fetch_model();
		$contract->get_request();
		$contract_dao->insert($contract);
		changes::get_instance()->mark($this->mode);
		return $this->show_all();
	}

	function liberate()
	{
		$contract_dao = new contract_dao();
		$contract = $contract_dao->fetch(get($contract_dao->key));
		$contract->status = 'L';
		$contract_dao->update($contract);
		changes::get_instance()->mark($this->mode);
		return $this->show_all();
	}

	function block()
	{
		$contract_dao = new contract_dao();
		$contract = $contract_dao->fetch(get($contract_dao->key));
		$contract->status = 'B';
		$contract_dao->update($contract);
		changes::get_instance()->mark($this->mode);
		return $this->show_all();
	}

}

?>